package com.xc.shopping_font.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xc.shopping_font.common.Result;
import com.xc.shopping_font.entity.Customer;
import com.xc.shopping_font.mapper.CustomerMapper;
import com.xc.shopping_font.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    CustomerService  customerService;

    @Resource
    CustomerMapper customerMapper;


    @PostMapping("/login")
    public Result fontLogin(HttpServletRequest request, @RequestBody Customer customer, HttpSession session){

//        将页面提交的密码进行md5加密处理
        String password = customer.getPassword();

        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Customer::getUsername,customer.getUsername());

        Customer customer1 = customerService.getOne(queryWrapper);

        if(customer1 == null ){
            return Result.error("400","用户名不存在，请重新登录");
        }



        if(!customer1.getPassword().equals(password)){
            return Result.error("400","密码错误，请重新登录");
        }

        if(customer1.getStatus()==0){
            return Result.error("401","该用户已注销");
        }

        session.setMaxInactiveInterval(600);  //设置过期时间以秒为单位，会自动刷新

//        登录成功
        request.getSession().setAttribute("customer",customer1.getId());

        log.info("id={}",request.getSession().getAttribute("customer"));

        return Result.success(customer1);
    }


    @PostMapping("/logout")
    public Result logout(HttpServletRequest request){

        request.getSession().removeAttribute("customer");

        return Result.success("退出成功");

    }

    @GetMapping("/{username}")
    public Result getPerson(@PathVariable String username){


        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("username",username);

        Customer customer  =    customerMapper.selectOne(queryWrapper);

        return Result.success(customer);

    }

    @PostMapping
    public Boolean saveCustomer(@RequestBody Customer customer){
        return customerService.saveOrUpdate(customer);
    }

    @PostMapping("/register")
    public Boolean addCustomer(@RequestBody Customer customer){

        String username = customer.getUsername();
        String password = customer.getPassword();

        password = DigestUtils.md5DigestAsHex(password.getBytes());

        customer.setPassword(password);

        customer.setStatus(1);

        if (!Strings.isNotEmpty(username) || !Strings.isNotEmpty(password)){
            return false;
        }

        else {
            return customerService.save(customer);
        }

    }




}
